package com.lagou.frame;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;

/**
 * @ClassName MyWebApplicationInitializer
 * @Description TODO
 * @Auth ying.xiao
 * @Date 2021/3/3
 * @Version 1.0.0
 */
public class MyWebApplicationInitializer implements WebApplicationInitializer {

	private String protocol = DEFAULT_PROTOCOL;
	public static final String DEFAULT_PROTOCOL = "org.apache.coyote.http11.Http11NioProtocol";

	@Override
	public void onStartup(ServletContext servletContext) {
		System.out.println("初始化 MyWebApplicationInitializer");
		//通过注解的方式初始化Spring的上下文
		AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
//		//注册spring的配置类（替代传统项目中xml的configuration）
		ac.register(AppConfig.class);
//        ac.refresh();

		// Create and register the DispatcherServlet
		//基于java代码的方式初始化DispatcherServlet
		DispatcherServlet servlet = new DispatcherServlet(ac);
		ServletRegistration.Dynamic registration = servletContext.addServlet("/", servlet);
		registration.setLoadOnStartup(1);
		registration.addMapping("/*");

	}
}
